﻿using AsmShaderConverter.AsmShader;
using AsmShaderConverter.HlslShader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsmShaderConverter.AsmFunctions
{
    public class AsmFunctionMov : IConvertToHlsl
    {
        readonly string originalLineOfCode;
        private Register destination;
        private Register source1;
        private bool saturate;

        public Register GetDestinationRegister()
        {
            return this.destination;
        }


        public AsmFunctionMov(string lineOfCode, bool halfPrecision, bool saturate)
        {
            this.originalLineOfCode = lineOfCode;
            this.saturate = saturate;

            lineOfCode = lineOfCode.Replace(",", ""); // Remove commas.
            lineOfCode = lineOfCode.Trim(); // Remove dead space.
            var values = lineOfCode.Split(' '); // split

            switch (values.Length)
            {
                case 3:
                    this.destination = new Register(values[1], halfPrecision, Swizzle.StandardXYZW);
                    this.source1 = new Register(values[2], destination.swizzle);

                    source1.swizzle = Swizzle.CorrectSwizzle(source1.swizzle, destination.swizzle);

                    break;
                default:
                    throw new Exception("Function can´t be converted! " + lineOfCode);
            }
        }

        public string ToHLSL(AsmShaderFile asmShader)
        {
            Swizzle.SetSwizzleLengthToDestination(destination, source1);

            string format = (this.saturate) ? "{0} = saturate({1});" : "{0} = {1};";

            return String.Format(format, this.destination.ToHLSL(asmShader), this.source1.ToHLSL(asmShader));
        }


        public string ToHLSLWitchComment(AsmShaderFile asmShader)
        {
            return this.ToHLSL(asmShader) + "//" + this.originalLineOfCode;
        }
    }
}
